package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.*;
import com.example.springboot.entity.vo.DormUserRole;
import com.example.springboot.entity.vo.TeacherUserRole;
import com.example.springboot.mapper.SysRoleMapper;
import com.example.springboot.mapper.TeacherMapper;
import com.example.springboot.service.SysUserRoleService;
import com.example.springboot.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServIcelmpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Override
    public void addTea(Teacher teacher) {
        //新增用户
        //保存角色
        int insert=teacherMapper.insert(teacher);
        if (insert>0){
            SysUserRole role=new SysUserRole();
            role.setRoleId(teacher.getRoleId());
            role.setUserId(teacher.getUserId());
            sysUserRoleService.save(role);

        }
    }

    @Override
    public void updataTea(Teacher teacher) {

    }

    @Override
    public Teacher TesLogin(String username, String password) {
        QueryWrapper<Teacher> qw = new QueryWrapper<>();
        qw.eq("username", username);
        qw.eq("password", password);
        Teacher teacher = teacherMapper.selectOne(qw);
        if (teacher != null) {
            return teacher;
        } else {
            return null;
        }
    }

    /**
     * 分页查询 带角色
     */
    @Override
    public Page findList(Integer pageNum, Integer pageSize, String search) {
        Page page = new Page<>(pageNum, pageSize);
        QueryWrapper<Teacher> qw = new QueryWrapper<>();
        qw.like("user_id", search);
        Page teacherPage = teacherMapper.selectPage(page, qw);
        // 获取学生列表
        List<Teacher> teachers = teacherPage.getRecords();
        // 创建一个新的列表用于存储带有角色名称的学生信息
        List<TeacherUserRole> teacherUserRoles = new ArrayList<>();
        for (Teacher teacher : teachers) {
            // 查询每个学生的角色名称
            QueryWrapper<SysUserRole> query=new QueryWrapper<>();
            query.lambda().eq(SysUserRole::getUserId,teacher.getUserId());
            SysUserRole one=sysUserRoleService.getOne(query);
            String roleName = "未知角色"; // 默认角色名称
            if (one != null) {
                // 查询角色名称
                SysRole name = sysRoleMapper.selectById(one.getRoleId());
                if (name != null) {
                    roleName = name.getRoleName(); // 更新角色名称
                }
            }
            // 创建新的对象并设置角色名称
            TeacherUserRole teacherUserRole = new TeacherUserRole(teacher, roleName);
            teacherUserRoles.add(teacherUserRole);
        }
        // 创建一个新的Page对象用于返回
        Page<TeacherUserRole> resultPage = new Page<>(pageNum, pageSize);
        resultPage.setRecords(teacherUserRoles);
        resultPage.setTotal(teacherPage.getTotal());
        resultPage.setSize(teacherPage.getSize());
        resultPage.setCurrent(teacherPage.getCurrent());
        return resultPage;
    }

    @Override
    public int updateTeacher(Teacher teacher) {
        int i = teacherMapper.updateById(teacher);
        return i;
    }


}
